// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require("base");
	var dialog = require("dialog");
	var constant = require("constant");
	var leftMenu = require("../leftMenu/leftMenu");
	var router = require("../hash/pageRouter");
	var databind = require("databind");
	var css_detail = require("./pickupDelayDetail.css");
	var pickupDelayId;
	var html_oppose_pickup = require("./html_opposePickupDelayApplication.html");
	var js_platform_main = require("./platform_main.js");
	var tools = require("tools");
	var init = function(page) {
		pickupDelayId = page.params.pickupDelayId;
//		pickupId = 51;	
		$("#popdialog").html(html_oppose_pickup);	
		inquirePickupDelayApplicationDetail();
//		leftMenu.init(6);
//		js_platform_main.init(0);
		$(".left").removeClass("none");
		initEvent();
		router.init();
		
	};
	var initEvent = function() {
		//若未登录，则返回登陆页面
		if (base.getData("sid") == null || base.getData("sid") == "" || base.getData("sid") == undefined) {
			top.location.href = "../login/login.html";
		}

		//拒绝提货申请
		$("#pickupDelayDetail .opposeBtn").on("click", function(){
			dialog.showDialog($("#oppose_pickup_delay_dialog"));
			databind.fillForm($("#oppose_pickup_delay_dialog"),{pickupDelayId:pickupDelayId});
			tools.inquireCommonSelectorList(9, $(".applicantReson"));
			$("#oppose_pickup_delay_dialog .sure_cancel").unbind("click").click(function(){
				dialog.hideDialog($("#oppose_pickup_delay_dialog"));
			});
			$("#oppose_pickup_delay_dialog .sure_yes").unbind("click").click(function(){
				databind.submitDom($("#oppose_pickup_delay_dialog"), function(){
					base.alert(1, "拒绝提货成功！");
					dialog.hideDialog($("#oppose_pickup_delay_dialog"));
					router.route("main/pickupDelayList.html");
				});
			});
		});
		
		//同意延期提货申请
		$("#pickupDelayDetail .agreeBtn").on("click", function(){
			router.route("main/pickupDelayConsent.html?pickupDelayId="+pickupDelayId);
		});
	};
	
	//提货申请列表
	var inquirePickupApplicationDetail = function(pickupId) {
		base.fnAjax({
			action: "inquireNormalPickupApplicationDetail.json",
			oData: {
				pickupId: pickupId
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					databind.fillForm($("#pickupDelayDetail2"), data.datas[0]);
					databind.fillForm($("#pickupDelayDetail1"),data.datas[0]);
					if($("#pickupDetailDeliveryMethod").html()==2){
						$("#pickupDetailDeliveryMethod").html("到港");
					}else if($("#pickupDetailDeliveryMethod").html()==1){
						$("#pickupDetailDeliveryMethod").html("到门");
					}
					
					if($("#pickupDetailPickupMethod").html()==1){
						$("#pickupDetailPickupMethod").html("现金提货");
					}else if($("#pickupDetailPickupMethod").html()==2){
						$("#pickupDetailPickupMethod").html("换货");
					}
					
					juicer.register("getValueByKey", constant.getValueByKey);
					
					var tpl1 = $("#pickupDetailBulkstockTpl").html();
					var html1 = juicer(tpl1, data);
					$("#pickupDetailBulkstockBody").html(html1);
					
					var tpl2 = $("#pickupDetailInventoryTpl").html();
					var html2 = juicer(tpl2, data);
					$("#pickupDetailInventoryBody").html(html2);
					
					if($("#pickupDetailInventoryBody").find("tr").length==1){
						$("#pickupDetailInventoryBody").parent().addClass("none");
					}
					if($("#pickupDetailBulkstockBody").find("tr").length==1){
						$("#pickupDetailBulkstockBody").parent().addClass("none");
					}
				} else {
					base.alert(2, "信息获取失败");
				}
			}
		});
	}

	//延期提货申请明细
	var inquirePickupDelayApplicationDetail = function() {
		base.fnAjax({
			action: "inquirePickupDelayApplicationDetail.json",
			oData: {
				pickupDelayId: pickupDelayId
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					databind.fillForm($("#pickupDelayDetail2"), data.datas[0]);
					databind.fillForm($("#oppose_pickup_delay_dialog"), data.datas[0]);
					tools.inquireCommonSelectorList(9, $(".delayReson"));
					inquirePickupApplicationDetail(data.datas[0].pickupId);
					
					juicer.register("getValueByKey", constant.getValueByKey);
					
					var tpl1 = $("#pickupDelayDetailBulkstockTpl").html();
					var html1 = juicer(tpl1, data);
					$("#pickupDelayDetailBulkstockBody").html(html1);
					
					var tpl2 = $("#pickupDelayDetailInventoryTpl").html();
					var html2 = juicer(tpl2, data);
					$("#pickupDelayDetailInventoryBody").html(html2);
					
					$(".delayReson").val(data.datas[0].delayReson);
					
					if($("#pickupDelayDetailBulkstockBody").find("tr").length==1){
						$("#pickupDelayDetailBulkstockBody").parent().addClass("none");
					}
					if($("#pickupDelayDetailInventoryBody").find("tr").length==1){
						$("#pickupDelayDetailInventoryBody").parent().addClass("none");
					}
					
					$("#pickupDelayDetailInventoryBody").on("click", ".detail", function(){
						router.route("bulkstockTallyDetail/bulkstockTallyDetail.html?inventoryId="+$(this).parents("tr").attr("inventoryId"));
					});
					
					databind.disableInput($("#pickupDelayDetail2"));
				} else {
					base.alert(2, "信息获取失败");
				}
			}
		});
	}

	return {
		init: init
	};
});